﻿// -----------------------------------------------------------------------
// <copyright file="Players.cs" company="DarkBone">
// DarkBone
// </copyright>
// -----------------------------------------------------------------------

namespace DarkBone.Engine.Collections
{
   using System.Collections.Generic;
   using System.Linq;

   /// <summary>
   /// Contains collection of players
   /// </summary>
   public sealed class Players : List<Player>
   {
      /// <summary>
      /// Updates player in collection
      /// </summary>
      /// <param name="playerNew">Player to update</param>
      /// <param name="getUnitsOfOwner">List of units of owner</param>
      public void AddOrUpdate(Player playerNew, List<Unit> getUnitsOfOwner)
      {
         var index = this.IndexOf(playerNew);
         playerNew.Units = getUnitsOfOwner;
         var categories =
            from p in getUnitsOfOwner
            group p by p.Template.DefaultType into g
            select new UnitSummary(g.Key, g.Count());

         playerNew.UnitSummary = categories;
         if (index < 0)
         {
            this.Add(playerNew);
            return;
         }

         var armySize = this[index].ArmySize;
         this[index] = playerNew;
         this[index].ArmySize = armySize;
      }
   }
}
